
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const MyPlugin = require('./myPlugin.js');

module.exports = {
  entry: './src/main.js',
  output: {
    filename: 'bundle.js',
    path: path.join(__dirname, 'dist'),
  },
  devServer: {
    contentBase: './public',
    proxy: {
      '/api': {
        target: 'https://api.github.com',
        // http://localhost:8080/api/users=>https://api.github.com/users
        pathRewrite: { '^/api': '' },
        changeOrigin: true
      }
    }
  },
  devtool: 'source-map',
  module: {
    rules: [
      {
        test: /.js$/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      },
      {
        test: /.css$/,
        use: [// 从后往前执行
          'style-loader',
          'css-loader'
        ]
      },
      {
        test: /.+[png|jpg]$/,
        use: {
          loader: 'url-loader',
          options: {
            limit: 10 * 1024
          }
        }
      },
      {
        test: /.html$/,
        use: {
          loader: 'html-loader',
          options: {
            // esModule: false
          }
        }
      },
      {
        test: /.md$/,
        use: [
          'html-loader',
          './markdownLoader.js'
        ]
      }
    ]
  },
  plugins: [
    // 清空文件
    new CleanWebpackPlugin(),
    // 默认生成 index.html
    new HtmlWebpackPlugin({
      title: 'my project',
      template: '!!ejs-loader?esModule=false!./src/template.html'
    }),
    new HtmlWebpackPlugin({
      filename: 'about.html'
    }),
    // 拷贝文件
    new CopyWebpackPlugin({
      patterns: [
        {
          from: 'public',
          to: 'public'
        }
      ]
    }),
    new MyPlugin()
  ]
}